#pragma once
#include <mutex> 
//
template< typename T >
class  CSingle
{
public:
	static T& Instance(void)
	{
		static T singleton;
		return singleton;
	}
private:
	~CSingle(void)
	{
	}

	CSingle(void)
	{
	}
};


template<typename T>
class CMySingle
{
public:
	static T* Instance()
	{
		if (0 == m_pInstance) 
		{
			std::unique_lock<std::mutex> lck(m_mutexSingleton);
			if (0 == m_pInstance)
			{
				m_pInstance = new T();
				atexit(Destroy);
			}
		}
		return m_pInstance;
	}
protected:
	CMySingle(){}
	~CMySingle(){}
private:
	CMySingle(const CMySingle&); 
	CMySingle& operator=(const CMySingle&);
	static void Destroy()
	{
		if ( m_pInstance != 0 )
		{
			delete m_pInstance;
			m_pInstance = 0;
		}
	}
	static std::mutex m_mutexSingleton;
	static T * volatile m_pInstance;
};
template<typename T>
std::mutex CMySingle<T>::m_mutexSingleton;
template<typename T>
T * volatile CMySingle<T>::m_pInstance = 0;